function [rvalue] = subsref(hFigure, S)
% BVQXfigure::subsref  - retrieve property via struct notation
%
% FORMAT:       propvalue = FigureObject.PropertyName
%
% Also, the subsref construct can be used as an alternative way of
% calling an object method:
%
% FORMAT:       hFigure.MethodName([Arguments]);
%
% Since method names are checked first, Parent() returns the parent
% object reference, not MABLAB's parent GUI handle !

% Version:      v0.7b
% Build:        7083122
% Date:         Aug-31 2007, 10:57 PM CEST
% Author:       Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL / Info:   http://wiki.brainvoyager.com/BVQXtools

% get method names for alias properties
global bvqxfigure_methods;
if isempty(bvqxfigure_methods)
    try
        methods(hFigure);
    catch
        % do nothing
    end
end

% argument check
if isempty(S)
    error( ...
        'BVQXfigure:BadSubsRef', ...
        'S struct may not be empty.' ...
    );
end
slen  = length(S);
stype = S(1).type;
ssubs = S(1).subs;

% decide on kind of subscripting, first struct-like notation
switch stype
    case {'.'}

        % check for subscript type
        if ~ischar(ssubs) || ...
            isempty(ssubs)
            error( ...
                'BVQXfigure:BadSubsRef', ...
                'Struct notation needs a non-empty char property.' ...
            );
        end

        % only works for singular object
        if numel(hFigure) ~= 1
            error( ...
                'BVQXfigure:InvalidInputSize', ...
                'Struct notation works only on singular objects.' ...
            );
        end

        % make content linear
        ssubs = ssubs(:)';

        % try to retrieve value
        try
            if any(strcmpi(ssubs, fieldnames(bvqxfigure_methods.m)))
                if slen > 1 && ...
                    strcmp(S(2).type, '()')
                    fargs = S(2).subs;
                    S(2)  = [];
                    slen  = length(S);
                else
                    fargs = {};
                end
                rvalue = BVQXfigure(hFigure, lower(ssubs), fargs{:});
            else
                rvalue = BVQXfigure(hFigure, 'Get', ssubs);
            end
        catch
            rethrow(lasterror);
        end

        % more sub-indexing ?
        if slen > 1
            try
                if slen > 2 && ...
                    strcmpi(ssubs, 'tagstruct') && ...
                    strcmp(S(2).type, '.') && ...
                    ischar(S(2).subs)
                    rvalue = rvalue.(S(2).subs);
                    rvalue = subsref(rvalue, S(3:end));
                else
                    rvalue = subsref(rvalue, S(2:end));
                end
            catch
                error( ...
                    'BVQXfigure:IllegalSubsRef', ...
                    'Couldn''t pass further subscripting to property value.' ...
                );
            end
        end

    % indexing requested
    case {'()'}

        % we need non-empty cell subscript
        if ~iscell(ssubs) || ...
            isempty(ssubs)
            error( ...
                'BVQXfigure:BadSubsRef', ...
                'Can''t index into BVQXfigure matrix.' ...
            );
        end

        % convert hFigure to struct
        sFigure = struct(hFigure);

        % try to retrieve subscripted matrix
        try
            subset  = subsref(sFigure, S(1));
            hFigure = class(subset, 'BVQXfigure');
        catch
            error( ...
                'BVQXfigure:BadSubsRef', ...
                'Invalid subscript error (%s).', ...
                lasterr ...
            );
        end

        % return sub-matrix if only one subscript
        if slen == 1
            rvalue = hFigure;
            return;
        end

        if numel(hFigure) ~= 1
            error( ...
                'BVQXfigure:InvalidObjectSize', ...
                'Further subscripting is only valid for singular objects.' ...
            );
        end

        % try to pass subscripts
        try
            rvalue = subsref(hFigure, S(2:end));
        catch
            rethrow(lasterror);
        end

    otherwise
        error( ...
            'BVQXfigure:BadSubsRef', ...
            'Only struct notation allowed to retrieve values.' ...
        );
end
